# Outputs to STDOUT the currently supported Meterpreter commands as JSON for the currently opened Meterpreter sessions
# Usage:
# msf> resource scripts/resource/meterpreter_compatibility.rc

<ruby>
require 'json'

# Attempt to load all known extensions
framework.sessions.values.map do |session|
  next unless session.type == 'meterpreter'

  Rex::Post::Meterpreter::ExtensionMapper.get_extension_names.sort.each do |extension_name|
    puts "[#{Time.now}][#{extension_name}] Starting to loading extension"
    session.core.use(extension_name)
    puts "[#{Time.now}][#{extension_name}] Loaded extension"
  rescue ::RuntimeError, ::MetasploitPayloads::Error
    puts "[#{Time.now}][#{extension_name}] Failed loading"
    # noop
  end
end

# Create an array of supported session information
session_data = framework.sessions.values.map do |session|
  next unless session.type == 'meterpreter'

  supported_commands = session.commands.map do |command_id|
    command_name = Rex::Post::Meterpreter::CommandMapper.get_command_name(command_id)

    {
      id: command_id,
      name: command_name
    }
  end

  {
    session_type: session.session_type,
    commands: supported_commands
  }
end.compact

result = {
  sessions: session_data
}

puts "[#{Time.now}] Generating result:"
puts JSON.fast_generate(result)
</ruby>
